<script language="javascript" type="text/javascript">

    function submit_item() {
        try {
            CalculateTotal();

            var namas = jQuery('.nama');
            var items = jQuery('.item');
            var qtys = jQuery('.qty');
            var harga_juals = jQuery('.price');
            var harga_belis = jQuery('.price_beli');
            var satuans = jQuery('.satuan');
            var totals = jQuery('.total');
            var margins = jQuery('.margin');
            jQuery.each(totals, function(i){
                if(jQuery(qtys[i]).val() != 0)
                    parent.parent.append_table(
                {
                    nama : jQuery(namas[i]).val(),
                    item: jQuery(items[i]).html(),
                    qty: jQuery(qtys[i]).val(),                    
                    harga_jual: jQuery(harga_juals[i]).val(),
                    harga_beli: jQuery(harga_belis[i]).val(),
                    satuan: jQuery(satuans[i]).html(),
                    total: jQuery(totals[i]).html(),
                    margin: jQuery(margins[i]).html()
                }
            );
            });
        }
        catch(e) { alert(e.message); }
        parent.parent.focus();

        close_gb();

    }

    function close_gb(){
<?php echo CloseGB(false); // close greybox   ?>
    }

    function UpdateTotals(id) {
        var qty = jQuery("#qty_"+id).val();
        var harga_jual = jQuery("#price_"+id).val();
        var harga_beli = jQuery("#price_beli_"+id).val();
        jQuery("#total_"+id).html(parseInt(qty) * parseFloat(harga_jual));
        jQuery("#margin_"+id).html(parseInt(qty) * (parseFloat(harga_jual) - parseFloat(harga_beli)));
        CalculateTotal();
    }

    function CalculateTotal(){
        var totals = jQuery('.total');
        var grandTotal = 0;
        jQuery.each(totals, function(i){
            grandTotal += parseFloat(jQuery(totals[i]).html());
        });
        jQuery('#grandtotal').html(parseFloat(grandTotal));

        var margins = jQuery('.margin');
        var grandTotalMargin = 0;
        jQuery.each(margins, function(i){
            grandTotalMargin += parseFloat(jQuery(margins[i]).html());
        });
        jQuery('#grandtotalmargin').html(parseFloat(grandTotalMargin));
    }
</script>

<h3 class="title-popup">Master Item</h3>

<div style="padding:10px">
    <div id="content">
        <div class="DivTable">
            <form name="fCari" id="fCari" action="" method="get" >
                <table class="GridTableFilter">
                    <tr>
                        <td>
                            <?php
                            echo $fp;
                            ?>
                        </td>
                    </tr>
                </table>
            </form>
            <br class="bersih" />

            <table class="GridTable" style="width: 100%;">
                <tr class="GridHeader">
                    <td>No.</td>
                    <td>Item</td>
                    <td>Jumlah</td>
                    <td>Harga Beli</td>
                    <td>Harga Jual</td>
                    <td>Total</td>
                    <td>Margin</td>
                </tr>
                <?php
                if ($rows > 0) {
                    $nomor = $offset;
                    foreach ($data as $row) {
                        $nomor++;
                        $styleTR = "GridItem1";
                        ?>
                <tr class="<?php echo $styleTR; ?>">
                    <td><?=$nomor?></td>
                    <td>
                        <?=form_input(array("id" => "nama".$row->nama_item, "class" => "nama"), $row->nama_item, "");?>
                        <span style="display: none;" class="item"><?=$row->id_item?></span>
                    </td>
                    <td><?=form_input(array("id" => "qty_$row->id_item", "class" => "qty"), 0,
                                        "onblur=\"UpdateTotals('$row->id_item');\" onkeyup=\"this.value=this.value.replace(/[^\d]/,'');\"");?>
                        &nbsp;
                        <span class="satuan"><?=$row->satuan_beli?></span>
                    </td>
                    <td align="right">
                        <span style="float: left;">Rp.
                                    <?=form_input(array("id" => "price_beli_".$row->id_item, "class" => "price_beli"), $row->harga_beli,
                                            "onblur=\"UpdateTotals('$row->id_item');\" ");?>
                        </span>
                    </td>
                    <td align="right">
                        <span style="float: left;">Rp.
                                    <?=form_input(array("id" => "price_".$row->id_item, "class" => "price"), $row->harga_jual,
                                            "onblur=\"UpdateTotals('$row->id_item');\" ");?>
                        </span>
                    </td>
                    <td align="right"><span style="float: left;">Rp. </span><span id="total_<?=$row->id_item?>" class="total">0</span></td>                    
                    <td align="right"><span style="float: left;">Rp. </span><span id="margin_<?=$row->id_item?>" class="margin">0</span></td>
                </tr>

                        <?php } ?>
                <tr class="GridHeader">
                    <td align="right" colspan="5"></td>
                    <td align="right"><span style="float: left;">Rp.  </span><span id="grandtotal">0</span></td>
                    <td align="right"><span style="float: left;">Rp.  </span><span id="grandtotalmargin">0</span></td>
                </tr>
                    <?php
                } else {
                    ?>
                <tr class="GridItem1">
                    <td colspan="7">Tidak ada data</td>
                </tr>

                    <?php
                }
                ?>
            </table>
        </div>
    </div>
    <?php echo $paging; ?>
    <br class="bersih" />
    <br class="bersih" />

    <div style="left: 0; bottom: 0; width: 100%; background: black; padding: 3px;">
        <button onclick="submit_item();">Save</button>&nbsp;
        <button onclick="close_gb();">Cancel</button>
    </div>

</div>